package com.common.test;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.log4testng.Logger;

	/**
		@author 王嘉犇
		@name selenium2失败截图功能
	 */

public class ScreenShot {
	
    private static Logger log = Logger.getLogger(ScreenShot.class);

    /**
     * 失败截图
     */
    
    public static void takeScreen(String errorclassname, WebDriver driver){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        String datestr = sdf.format(date);
        Random random = new Random();
        int x = random.nextInt(89999);
        x = x+10000;
        String filename = errorclassname + datestr + String.valueOf(x);
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        File filepath = new File("src/main/resources/screenshot");
        String absolutepath = filepath.getAbsolutePath();
        log.info("报错截图jpg文件名为 " + filename);
        try{
            FileUtils.copyFile(scrFile, new File(absolutepath + "/" + filename + ".jpg"));
        }catch (IOException e){
            log.info("meiyoushengcheng");
        }
    }
}
